
/**		
 * 
 *	uk.co.lmfm.guppy.view.components.project.ProjectLibraryView
 *	
 *	@version 1.00 | Jan 22, 2010
 *	@author Justin Windle
 *  
 **/
 
package uk.co.lmfm.guppy.view.components.project 
{
	import uk.co.lmfm.guppy.model.vo.ClassLibraryVO;
	import uk.co.lmfm.guppy.view.components.core.IViewComponent;

	import com.bit101.components.CheckBox;
	import com.bit101.components.Label;

	import flash.display.Sprite;
	import flash.utils.Dictionary;

	/**
	 * ProjectLibraryView
	 */
	public class ProjectLibraryView extends ProjectUIPanel implements IViewComponent 
	{
		//	----------------------------------------------------------------
		//	CONSTANTS
		//	----------------------------------------------------------------

		private static const COL_STEP : int = 120;
		private static const ROW_STEP : int = 20;

		//	----------------------------------------------------------------
		//	PRIVATE MEMBERS
		//	----------------------------------------------------------------

		private var _title : Label = new Label();
		private var _checkBoxes : Sprite = new Sprite();
		private var _libraryIndex : Dictionary = new Dictionary();

		//	----------------------------------------------------------------
		//	CONSTRUCTOR
		//	----------------------------------------------------------------

		public function ProjectLibraryView()
		{
			super();
			
			_title.text = "Select required libraries:";
			_title.x = 10;
			_title.y = 5;
			
			_checkBoxes.x = _title.x;
			_checkBoxes.y = _title.y + _title.height + 10;
			
			addChild(_checkBoxes);
			addChild(_title);
		}

		//	----------------------------------------------------------------
		//	PUBLIC METHODS
		//	----------------------------------------------------------------

		public function clearClassLibraries() : void
		{
			while(_checkBoxes.numChildren > 0)
			{
				_checkBoxes.removeChildAt(0);
			}
			
			_libraryIndex = new Dictionary();
		}

		public function addClassLibrary(library : ClassLibraryVO) : void
		{
			var checkBox : CheckBox = new CheckBox();
			checkBox.label = library.name + ' (' + library.version + ')';
			
			_libraryIndex[checkBox] = library;
			_checkBoxes.addChild(checkBox);
		}

		//	----------------------------------------------------------------
		//	PROTECTED METHODS
		//	----------------------------------------------------------------

		override protected function resize() : void
		{
			super.resize();
			
			height = 155;
			
			var cb : CheckBox;
			var xpos : int = 0;
			var ypos : int = 0;
			
			for (var i : int = 0;i < _checkBoxes.numChildren;i++) 
			{
				cb = _checkBoxes.getChildAt(i) as CheckBox;
				
				cb.x = xpos;
				cb.y = ypos;
				
				ypos += ROW_STEP;
				
				if(ypos >= height - _checkBoxes.y - 20)
				{
					ypos = 0;
					xpos += COL_STEP;
				}
			}
		}

		//	----------------------------------------------------------------
		//	PUBLIC ACCESSORS
		//	----------------------------------------------------------------

		public function get selected() : Vector.<ClassLibraryVO>
		{
			var selected : Vector.<ClassLibraryVO> = new Vector.<ClassLibraryVO>();
			var checkbox : CheckBox;
			
			for (var i : int = 0;i < _checkBoxes.numChildren;i++)
			{
				checkbox = _checkBoxes.getChildAt(i) as CheckBox;
				
				if(checkbox.selected)
				{
					selected.push(_libraryIndex[checkbox]);
				}
			}
			
			return selected;
		}
	}
}
